package com.cqfae.deploycentral.app.config;

import com.cqfae.deploycentral.app.dao.entity.JobInfo;
import com.cqfae.deploycentral.app.dao.mapper.JobInfoMapper;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Properties;

/**
 * Created by JIANGLU009 on 2017/11/28.
 */
@Configuration
public class JobExecuteConfig {

    private static Logger logger = LoggerFactory.getLogger(ConstantsCacheConfig.class);

    @Autowired
    private JobInfoMapper jobInfoMapper;

    @Autowired
    private JobExecuteInfo jobExecuteInfo;

    @Autowired
    private DataSource dataSource;

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        logger.info("==== Loading job config Start ====");
        List<JobInfo> list = jobInfoMapper.selectAll();
        for (JobInfo jobInfo : list) {
            String jobName = jobInfo.getJobName();
            String methodStr = "setJobInfo" + jobName.substring(0, 1).toUpperCase() + jobName.substring(1);
            Class classType = JobExecuteInfo.class;
            try {
                Method method = classType.getMethod(methodStr, new Class[]{JobInfo.class});
                method.invoke(jobExecuteInfo, jobInfo);
            } catch (NoSuchMethodException e) {
                logger.error("JobExecuteConfig NoSuchMethodException {}", e);
            } catch (InvocationTargetException e) {
                logger.error("JobExecuteConfig InvocationTargetException {}", e);
            } catch (IllegalAccessException e) {
                logger.error("JobExecuteConfig IllegalAccessException {}", e);
            }

        }
        logger.info("==== Loading job config End ====");
    }

    /**
     * quartz持久化配置
     *
     * @return
     * @throws SchedulerException
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean getSchedulerFactoryBean() throws SchedulerException {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(dataSource);
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
        schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(true);
        Properties properties = new Properties();
        properties.setProperty("org.quartz.scheduler.instanceId", "AUTO");
        properties.setProperty("org.quartz.scheduler.instanceName", "JobCluster");
        properties.setProperty("org.quartz.jobStore.misfireThreshold", "60000");
        properties.setProperty("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
        properties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
        properties.setProperty("org.quartz.jobStore.isClustered", "true");
        properties.setProperty("org.quartz.jobStore.useProperties", "false");
        properties.setProperty("org.quartz.jobStore.tablePrefix", jobExecuteInfo.getQuartzTablePrefix());
        properties.setProperty("org.quartz.jobStore.clusterCheckinInterval", "2000");
        schedulerFactoryBean.setQuartzProperties(properties);
        return schedulerFactoryBean;
    }
}
